package com.lagou.goods.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lagou.common.pojo.Goods;
import com.lagou.common.pojo.GoodsQuery;
import com.lagou.goods.service.GoodsService;
import com.netflix.discovery.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping("/query/{id}")
    public Goods queryGoodsById(@PathVariable Integer id) {
        System.out.println("goods = " + 9200);
        return goodsService.queryGoodsById(id);
    }

    @GetMapping("/delete/{id}")
    public String deleteGoodsById(@PathVariable Integer id) {

        if (goodsService.deleteGoodsById(id) > 0) {
            return "删除商品成功";
        } else {
            return "删除商品失败！";
        }
    }

    @PostMapping("/update")
    public String updateGoodsById(@RequestBody Goods goods) {
        Integer i = goodsService.updateGoodsById(goods);

        if (i > 0) {
            return "修改成功";
        } else {
            return "修改失败";
        }
    }


    @PostMapping("query/{current}/{size}")
    public Map queryByPage(@PathVariable Integer current, @PathVariable Integer size, @RequestBody(required = false) GoodsQuery goodsQuery) {

        //创建一个page对象
        Page<Goods> page = new Page<>(current,size);
        //构建条件
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();

        if (goodsQuery != null){
            String name = goodsQuery.getName();
            String type = goodsQuery.getType();
            Integer maxPrice = goodsQuery.getMaxPrice();
            Integer minPrice = goodsQuery.getMinPrice();
            Integer maxStock = goodsQuery.getMaxStock();
            Integer minStock = goodsQuery.getMinStock();
            String status = goodsQuery.getStatus();
            System.out.println("goodsQuery = " + goodsQuery);

            //判断条件值是否为空,如果不为空,拼接条件
            if (!StringUtils.isEmpty(name)) {
                System.out.println("name = " + name);
                queryWrapper.like("name", name);
            }
            if (!StringUtils.isEmpty(type)) {
                System.out.println("status = " + status);
                queryWrapper.eq("goods_type", type);
            }
            if (null !=maxPrice) {
                queryWrapper.le("price", maxPrice);
            }
            if ( null !=minPrice) {
                queryWrapper.gt("price", minPrice);
            }
            if (null !=maxStock) {
                queryWrapper.le("goods_stock", maxStock);
            }
            if (null !=minStock) {
                queryWrapper.gt("goods_stock", minStock);
            }
            if (!StringUtils.isEmpty(status)){
                queryWrapper.eq("status",status);
            }

        }

        Page<Goods> goodsPage = goodsService.queryByPage(page, queryWrapper);
        long total = goodsPage.getTotal();
        List<Goods> records = goodsPage.getRecords();
        HashMap<Object, Object> map = new HashMap<>();
        map.put("list", records);
        map.put("total", total);
        return map;

    }



}
